<template>
  <div class="swiper-container" ref="swiper">
    <!-- 轮播图容器 -->
    <div class="swiper-wrapper">
      <!-- 轮播图 -->
      <div class="swiper-slide" v-for="(img, index) in imageList" :key="index">
        <img :src="img.imgUrl" :alt="img.imgName" />
      </div>
    </div>
    <!-- 左右箭头 -->
    <div class="swiper-button-next"></div>
    <div class="swiper-button-prev"></div>
    <!-- 小圆点 -->
    <div class="swiper-pagination"></div>
  </div>
</template>

<script>
import Swiper from "swiper";
import "swiper/css/swiper.min.css";

export default {
  name: "Carousel",
  data() {
    return {
      isInit: false,
    };
  },
  props: {
    imageList: {
      type: Array,
      default: () => [],
    },
  },
  watch: {

    imageList: {
      handler(imageList) {
        // 让第一个轮播图第一次不要new Swiper
        if (!imageList.length || this.isInit) return;
        this.isInit = true;
        this.$nextTick(() => {
          const mySwiper = new Swiper(this.$refs.swiper, {
            // 左右箭头
            navigation: {
              nextEl: ".swiper-button-next",
              prevEl: ".swiper-button-prev",
            },
            // 小圆点
            pagination: {
              el: ".swiper-pagination",
            },
            // 自动轮播
            autoplay: {
              delay: 1000, // 自动轮播的时间
              disableOnInteraction: false, // 用户操作完轮播图后，再次开启自动轮播
            },
            // 无缝轮播
            loop: true,
          });

          mySwiper.el.onmouseenter = () => {
            mySwiper.autoplay.stop();
          };
          mySwiper.el.onmouseleave = () => {
            mySwiper.autoplay.start();
          };
        });
      },
      // 解决floor组件轮播图失效
      immediate: true,
    },
  },
  
};
</script>

<style></style>